package com.sdd.sdr;

import com.alibaba.csp.sentinel.slots.block.SentinelRpcException;
import com.sdd.sdr.api.OtherService;
import com.sdd.sdr.consumer.UserConsumer;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * ClassName: DynamicRuleConsumerApplication
 * Description:
 * date: 2020/7/2
 *
 * @author xugaojun
 */
@SpringBootApplication
@EnableDubbo(scanBasePackages = "com.sdd.sdr.consumer")
public class DynamicRuleConsumerApplication {

    public static void main(String[] args) {

        ConfigurableApplicationContext context = SpringApplication.run(DynamicRuleConsumerApplication.class);

        UserConsumer userConsumer = context.getBean(UserConsumer.class);
        OtherService otherService = context.getBean(OtherService.class);

        for (int i = 0; i < 15; i++) {
            try {
                System.out.println(userConsumer.saveUser());
            } catch (SentinelRpcException e) {
                System.out.println("Blocked!");
            } catch (Exception e) {
                System.out.println("Blocked!");
            }
        }

        for (int i = 0; i < 15; i++) {
            try {
                System.out.println(userConsumer.getUser());
            } catch (SentinelRpcException e) {
                System.out.println("Blocked!");
            } catch (Exception e) {
                System.out.println("Blocked!");
            }
        }

        for (int i = 0; i < 15; i++) {
            otherService.doOtherThing();
        }

        System.exit(1);
    }

}
